// -----------------------------------------------------------------------
// <copyright file="Node.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.ENTITIES
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using AdCom.Utility;
    using VDC.COMMON;
    
    /// <summary>
    /// Class Node
    /// </summary>
    public class Node
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Node" /> class
        /// </summary>
        public Node()
        {
            this.NodeId = 0;
            this.NodeTypeId = 0;
            this.Code = string.Empty;
            this.Name = string.Empty;
        }

        /// <summary>
        /// Gets or sets NodeId
        /// </summary>
        public int NodeId { get; set; }

        /// <summary>
        /// Gets or sets NodeTypeId
        /// </summary>
        public int NodeTypeId { get; set; }

        /// <summary>
        /// Gets or sets Code
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// Gets or sets Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets NodeType
        /// </summary>
        public NodeType NodeType
        {
            get
            {
                try
                {
                    NodeType nodeType = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneNodeTypeGetById, this.NodeTypeId));

                    while (reader.Read())
                    {
                        nodeType = new NodeType();

                        if (!reader.IsDBNull(0))
                        {
                            nodeType.NodeTypeId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            nodeType.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            nodeType.Name = reader.GetString(2);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return nodeType;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets NodeWorkflows
        /// </summary>
        public IList<NodeWorkflow> NodeWorkflows
        {
            get
            {
                try
                {
                    IList<NodeWorkflow> lstNodeWorkflow = new List<NodeWorkflow>();
                    var reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneNodeWorkflowGetByNodeId, this.NodeId));
                    while (reader.Read())
                    {
                        NodeWorkflow nodeWorkflow = new NodeWorkflow();
                        if (!reader.IsDBNull(0))
                        {
                            nodeWorkflow.NodeWorkflowId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            nodeWorkflow.WorkflowId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            nodeWorkflow.NodeId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            nodeWorkflow.ConditionsId = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            nodeWorkflow.NextNodeId = reader.GetInt32(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            nodeWorkflow.ProfileTypeProgressId = reader.GetInt32(5);
                        }

                        if (!reader.IsDBNull(6))
                        {
                            nodeWorkflow.ProfileStatusId = reader.GetInt32(6);
                        }

                        lstNodeWorkflow.Add(nodeWorkflow);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstNodeWorkflow;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }
    }
}
